import random
import time

from selenium import webdriver
from selenium.common import NoSuchElementException

driver = webdriver.Chrome()
driver.get("https://www.moguoyunke.com")
# 将浏览器窗口最大化
driver.maximize_window()
time.sleep(1)
# 登录
driver.find_element("xpath", '//button[text()="登录"]').click()  # 点击登录
time.sleep(2)
driver.find_element("xpath", '//*[text()=" 账号密码 "]').click()  # 点击账号密码按钮
time.sleep(1)
# 定位登录账号文本框
ele_user = driver.find_element("xpath", '//input[@tabindex="1"]')
time.sleep(1)
ele_user.click()  # 点击“账号”文本框
ele_user.send_keys("15613043260")  # 输入登录账号
time.sleep(1)
# 定位登录密码文本框
ele_user = driver.find_element("xpath", '//input[@tabindex="2"]')
time.sleep(1)
ele_user.click()  # 点击“密码”文本框
time.sleep(1)
ele_user.send_keys("Zhixin@2025")  # 输入登录密码
time.sleep(1)
# 勾选“我已阅读并同意服务协议和隐私政策”复选框
driver.find_element("xpath", '//div/p[@class="protocol-box"][2]/label').click()
# 点击登录按钮
time.sleep(1)
driver.find_element("xpath", '//span[text()=" 登录 "]').click()
time.sleep(0.3)
msg = driver.find_element("xpath", '//p[@class="el-message__content"]').text
print(msg)
assert "登录成功" == msg
time.sleep(1)
# 点击首页“+”
driver.find_element("xpath", '//*[@class="edit-btn"]').click()
time.sleep(0.5)
# 获取我的分类列表
my_classify_list = []
my_classify_ele = driver.find_elements("xpath", '//*[@class="my-classify-item"]')
if my_classify_ele:
    for i in my_classify_ele:
        my_classify_list.append(i.text)
        print(i.text, end="\t")
# 删除存在的分类
driver.find_element("xpath", f'//section[@class="el-drawer__body"]//div[text()=" {my_classify_list[0]} "]/img').click()
my_classify_list.pop(0)
print("删除分类后的列表显示", my_classify_list)
# 添加语言学习分类
my_subclass_ele_list = driver.find_elements("xpath", '//ul[@class="subclass-list"]')
if my_subclass_ele_list:
    for ele in my_subclass_ele_list:
        ele.click()
        my_classify_list.append(ele.text)
# 添加分类成功后，获取我的分类列表
print("添加完分类后列表显示", my_classify_list)
# 点击完成按钮
driver.find_element("xpath", '//button[text()=" 完成 "]').click()
time.sleep(0.3)
msg = driver.find_element("xpath", '//p[@class="el-message__content"]').text
# print(msg)
try:
    assert "保存成功" == msg
except AssertionError as e:
    print(e)
time.sleep(1)
# 3.首页功能
# 点击推荐tab按钮
driver.find_element("xpath", '//div[text()=" 推荐 "]').click()
time.sleep(1)
# 获取推荐tab下免费的课程
free_course_list = []
free_course_eles = driver.find_elements("xpath", '//div[@class="price-wrap"]/span[@class="free-price"]')
print("免费课程列表", free_course_eles)
if free_course_eles:
    for ele in free_course_eles:
        print(f"当前元素{ele}")

        time.sleep(1)
        # 点击其中的一门课程
        try:

            ele.click()
            #进入课程详情页
            time.sleep(1)
            # 获取所有窗口的句柄
            all_windows=driver.window_handles
            print("获取所有窗口的句柄", all_windows)
            # 切换到当前最新打开的窗口

            driver.switch_to.window(all_windows[-1])
            print("当前窗口的句柄", driver.current_window_handle)

            time.sleep(1)
            butt_ele=driver.find_element("xpath", '//div[@class="right"]/button')
            butt_ele_text=butt_ele.text
            print(f"按钮的文本:{butt_ele_text}")
            if butt_ele_text=="去学习":
                print("该课程已购买")
                driver.execute_script("window.close();")
                driver.switch_to.window(all_windows[0])
            else:
                print("课程还未领取，可以进行点击领取")
                butt_ele.click()
                time.sleep(0.2)
                input()
                msg=driver.find_element("xpath", '//div[@class="el-message-box__message"]/p').text
                assert msg=="报名成功，即将跳转到学习页面~"
                break
        except Exception as e:
            driver.execute_script("arguments[0].scrollIntoView()", ele)
            ele.click()



time.sleep(1)
input()
driver.close()
